package gui;

import domein.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;

/**
 *
 * @author Robin
 */
public class NieuweGebruiker extends javax.swing.JFrame implements Observer {

    /** Creates new form NieuweGebruiker */
    private WebinarController wc;

    public NieuweGebruiker() {
        wc = WebinarController.getInstance();
        wc.addObserver(this);

        //dimensions
        Toolkit tk = Toolkit.getDefaultToolkit();


        Dimension screenSize = tk.getScreenSize();
        int screenwidth = screenSize.width;

        setLocation((screenwidth - 400) / 2, 0);
        initComponents();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblwwmatch1 = new javax.swing.JLabel();
        txtVoornaam = new javax.swing.JTextField();
        txtFamilienaam = new javax.swing.JTextField();
        txtWw = new javax.swing.JPasswordField();
        txtWwb = new javax.swing.JPasswordField();
        lblvn = new javax.swing.JLabel();
        lblfn = new javax.swing.JLabel();
        lblww = new javax.swing.JLabel();
        lblwwb = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        button_annuleer1 = new javax.swing.JButton();
        button_registreren1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 24));
        jLabel4.setText("Nieuwe Gebruiker registreren");

        lblwwmatch1.setForeground(new java.awt.Color(255, 51, 0));
        lblwwmatch1.setText("Wachtwoorden zijn niet identiek!");
        lblwwmatch1.setVisible(false);

        lblvn.setForeground(new java.awt.Color(204, 51, 0));
        lblvn.setText(" ");

        lblfn.setForeground(new java.awt.Color(204, 51, 0));
        lblfn.setText(" ");

        lblww.setForeground(new java.awt.Color(204, 51, 0));
        lblww.setText(" ");

        lblwwb.setForeground(new java.awt.Color(204, 51, 0));
        lblwwb.setText(" ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtWw, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                            .addComponent(txtVoornaam, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                            .addComponent(txtFamilienaam, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(lblwwmatch1))
                            .addComponent(txtWwb, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(lblvn, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(lblfn, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(lblww, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(lblwwb, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(txtVoornaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblvn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtFamilienaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblfn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtWw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblww)
                .addGap(12, 12, 12)
                .addComponent(txtWwb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblwwb)
                .addGap(18, 18, 18)
                .addComponent(lblwwmatch1)
                .addGap(31, 31, 31))
        );

        button_annuleer1.setText("Annuleren");
        button_annuleer1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_annuleer1ActionPerformed(evt);
            }
        });

        button_registreren1.setText("Registreren");
        button_registreren1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_registreren1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(232, Short.MAX_VALUE)
                .addComponent(button_annuleer1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(button_registreren1))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_annuleer1)
                    .addComponent(button_registreren1)))
        );

        jLabel7.setText("Voornaam:");

        jLabel9.setText("Familienaam:");

        jLabel8.setText("Wachtwoord:");

        jLabel6.setText("Bevestig wachtwoord:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jLabel9)
                .addGap(45, 45, 45)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(40, 40, 40))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button_registreren1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_registreren1ActionPerformed
        lblvn.setText(" ");
        lblfn.setText(" ");
        lblww.setText(" ");
        lblwwb.setText(" ");
        if (!veldenIngevuld()) {
            if (txtVoornaam.getText().trim().equals("")) {
                lblvn.setText("Voornaam moet ingevuld zijn!");
            }

            if (txtFamilienaam.getText().trim().equals("")) {
                lblfn.setText("Familienaam moet ingevuld zijn!");
            }

            if (txtWw.getPassword().length == 0) {
                lblww.setText("Wachtwoord moet ingevuld zijn!");
            }

            if (txtWwb.getPassword().length == 0) {
                lblwwb.setText("Wachtwoord moet bevestigd zijn!");
            }

            if (!(txtWw.getText().trim().equals(txtWwb.getText().trim()))) {
                lblwwmatch1.setVisible(true);
            }
        } else {
            Gebruiker g = new Gebruiker(txtVoornaam.getText(), txtFamilienaam.getText(), txtWw.getText());
            try {
                wc.voegGebruikerToe(g);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "", JOptionPane.ERROR_MESSAGE);
            }
            dispose();
        }
    }//GEN-LAST:event_button_registreren1ActionPerformed

    private void button_annuleer1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_annuleer1ActionPerformed
        dispose();
    }//GEN-LAST:event_button_annuleer1ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_annuleer1;
    private javax.swing.JButton button_registreren1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblfn;
    private javax.swing.JLabel lblvn;
    private javax.swing.JLabel lblww;
    private javax.swing.JLabel lblwwb;
    private javax.swing.JLabel lblwwmatch1;
    private javax.swing.JTextField txtFamilienaam;
    private javax.swing.JTextField txtVoornaam;
    private javax.swing.JPasswordField txtWw;
    private javax.swing.JPasswordField txtWwb;
    // End of variables declaration//GEN-END:variables

    private boolean veldenIngevuld() {
        if (txtVoornaam.getText().trim().equals("")) {
            return false;
        }

        if (txtFamilienaam.getText().trim().equals("")) {
            return false;
        }

        if (txtWw.getPassword().length == 0) {
            return false;
        }

        if (txtWwb.getPassword().length == 0) {
            return false;
        }

        if (!(txtWw.getText().trim().equals(txtWwb.getText().trim()))) {
            return false;
        }
        return true;
    }

    //implementatie Observer pattern
    @Override
    public void update(Observable arg0, Object arg1) {
        if (arg1.equals("nieuweGebruiker")) {
            wc.updateGebruikers();
        }

    }
    //einde observer
}
